package com.crm.services;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.model.mdao.GatherguestsMapper;
import com.crm.model.mdao.InternatwatersMapper;
import com.crm.model.mdao.SysDeptMapper;
import com.crm.model.mpojo.Gatherguests;
import com.crm.model.mpojo.Internatwaters;
import com.crm.model.pojo.Customer;
import com.github.pagehelper.PageHelper;

/**
 * 公海获客服务类
 * @author Menghui
 *
 */
@Service
public class GatherguestService {
	@Autowired
	GatherguestsMapper gaMapper;//获客dao
	@Autowired 
	InternatwatersMapper inMapper;//公海dao
	@Autowired
	SysDeptMapper deptMapper;//部门dao
	
	/**
	 * 获取当天获客数
	 */
	public int selectgetcustNum() {
		return gaMapper.selectgetcustNum();
	}
	
	/**
	 * 查询所有获客信息
	 * @param pageNum
	 * @param pageSize
	 * @return
	 * @throws ServiceException
	 */
	public List<Map<String, String>> selectByPager(int pageNum, int pageSize) throws ServiceException {
		// 使用分页插件
		System.out.println("pageNum:" + pageNum + ",pageSize:" + pageSize);
		// 配置分页信息
		PageHelper.startPage(pageNum, pageSize);
		return gaMapper.selectAllGather();
	}
	
	/**
	 * 集客
	 * @param gatherguests
	 * @return 
	 */
	public int addGather(Gatherguests gatherguests) {
		//boolean flag = false;
		int insert = gaMapper.insertSelective(gatherguests);
		if(insert>0) {
			System.out.println("新增一条客户信息:"+gatherguests);
		}
		return insert;
	}
	
	/**
	 * 新建公海
	 * @param internatwaters
	 * @return
	 */
	public int addInter(Internatwaters internatwaters) {
		int insert = inMapper.insertSelective(internatwaters);
		if(insert>0) {
			System.out.println("新增一条公海信息:"+internatwaters);
		}
		return insert;
	}
	
	//回显公海数据
	public List<Internatwaters> selectInternat() {
		return inMapper.selectAllInternat();
	}
	
	/**
	 * 查询部门名称
	 * @return
	 */
	public List<Map<String, String>> selectDept() {
		return deptMapper.selectDeptName();
	}
	
	/**
	 * 转普通
	 * @return 
	 */
	public int updateCust(Integer gatherId) {
		return gaMapper.updateCust(gatherId);
	}
	
	/**
	 * 查询单条集客数据
	 * @return 
	 */
	public Gatherguests selectOne(Integer gaId) {
		return gaMapper.selectByPrimaryKey(gaId);
	}
	
	public Gatherguests selectGaid(Integer gaId) {
		return gaMapper.selectByGaid(gaId);
	}
	
	//导入公海客户
	public List<Gatherguests> importClient(Workbook workbook) {
		//新建一个临时保存公海客户的list集合
		ArrayList<Gatherguests> temp = new ArrayList<>();
		int i=1;
		Sheet sht0 = workbook.getSheetAt(0);
		try { 
			for (Row r : sht0) {
				if (r.getRowNum() < 1) {
					continue;
				}
			Gatherguests gatherguests=new Gatherguests();
			//获取数据
			if(r.getCell(0)!=null) {
				r.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
				gatherguests.setGaName(r.getCell(0).getStringCellValue());
				System.out.println("获取客户姓名："+r.getCell(0).getStringCellValue());
			}
			//gatherguests.setGaName(r.getCell(1).getRichStringCellValue().getString());
			gatherguests.setGaTel(getCellValue(r.getCell(1)).toString());
			System.out.println("获取客户电话号码："+getCellValue(r.getCell(1)).toString());
			gatherguests.setGaEmail(getCellValue(r.getCell(2)).toString());
			System.out.println("获取客户邮箱："+getCellValue(r.getCell(2)).toString());
			if(getCellValue(r.getCell(3)).toString().equals("男")) {
				gatherguests.setGaSex(1005);
			}else if (getCellValue(r.getCell(3)).toString().equals("女")) {
				gatherguests.setGaSex(1006);
			}
			String string = getCellValue(r.getCell(4)).toString();
			System.out.println("获取客户所有者："+string);
			int parseInt = Integer.parseInt(string);
			System.out.println("转成integer类型后："+parseInt);
			gatherguests.setuId(parseInt);
			gatherguests.setGaCreate(new Date(System.currentTimeMillis()));
			gatherguests.setGaUpdate(new Date(System.currentTimeMillis()));
			gatherguests.setGaDistribut(1441);
			gatherguests.setGaTransfer(1444);
			temp.add(gatherguests);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return temp;
	}
	
	/**
	 * CELL的转换
	 * @param cell
	 * @return
	 */
	private static  Object getCellValue(Cell cell) {
        Object value = null;
 
        DecimalFormat df = new DecimalFormat("0");//格式化number String字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//日期格式化
 
        switch (cell.getCellTypeEnum()) {
            case STRING:
                value = cell.getRichStringCellValue().getString();
                break;
            case NUMERIC:
                if("General".equals(cell.getCellStyle().getDataFormatString())){
                    value = df.format(cell.getNumericCellValue());
                }else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
                    value = sdf.format(cell.getDateCellValue());
                }else{
                    value = df.format(cell.getNumericCellValue());
                }
                break;
            case BOOLEAN:
                value = cell.getBooleanCellValue();
                break;
            case BLANK:
                value = "";
                break;
            default:
                value = cell.toString();
                break;
        }
        return value;
    }
	
	//导入客户到公海数据库
	public int addClient(List<Gatherguests> importClient) {
		int insertSelective =0; 
		try {
			for (Gatherguests gatherguests : importClient) {
				insertSelective = gaMapper.insertSelective(gatherguests);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return insertSelective;
	}
	
	//修改公海客户信息
	public int updateGather(Integer gatherId,Integer uId,Date gaUpdate) {
		int updateGather = gaMapper.updateGather(gatherId, uId, gaUpdate);
		if(updateGather>0) {
			System.out.println("公海客户所有者修改成功！");
		}else {
			System.out.println("公海客户所有者修改失败！");
		}
		return updateGather;
	}
	
	//修改公海客户信息
	public int updateGather2(Gatherguests gatherguests) {
		//gaMapper.updateByPrimaryKeySelective(gatherguests);
		return gaMapper.updateByPrimaryKey(gatherguests);
	}

		
	//根据所有者模糊查询
	public List<Map<String, String>> selelctUname(int pageNum, int pageSize,
			String uname) {
		PageHelper.startPage(pageNum, pageSize);
		return gaMapper.selectByuName(uname);
	}
}
